﻿using ST.Library.UI.NodeEditor;
using STNode_A2B.DataType;
using STNode_A2B.STNodeControls;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WinNodeEditorDemo.Blender;

namespace STNode_A2B.STNodes
{
    [STNode("A2B")]
    public class STNode_Slave_Node : STNode
    {
        private STNodeOption m_op_out_IIC;
        private STNodeOption m_op_out_Rx0;
        private STNodeOption m_op_out_Rx1;
        private STNodeOption m_op_out_Tx0;
        private STNodeOption m_op_out_Tx1;
        private STNodeOption m_op_out_B;
        private STNodeOption m_op_in_A;

        private STNodeControl_Label m_control_lbl_Rx_ch;
        private STNodeControl_Label m_control_lbl_Tx_ch;

        private STNodeControl_TextBox m_control_txt_Rx_ch;
        private STNodeControl_TextBox m_control_txt_Tx_ch;

        private STNodeSelectEnumBox m_control_selectbox;

        public enum ChipType
        {
            NCA2428,
            NCA2418
        }

        private ChipType _chipType;
        [STNodeProperty("ChipType", "ChipType")]
        public ChipType ChipMixType
        {
            get { return _chipType; }
            set
            {
                _chipType = value;
                m_control_selectbox.Enum = value;
                Invalidate();
            }
            
        }

        private int _Rx_ch_Data;
        [STNodeProperty("Rx-ch", "Rx-ch Data")]
        public string Rx_ch_Data
        {
            get { return _Rx_ch_Data.ToString(); }
            set
            {
                int temp;
                if (int.TryParse(value, out temp))
                {
                    m_control_txt_Rx_ch.EditorText = value;
                    _Rx_ch_Data = int.Parse(value);
                    Invalidate();
                }

            }
        }

        private int _Tx_ch_Data;
        [STNodeProperty("Tx-ch", "Tx-ch Data")]
        public string Tx_ch_Data
        {
            get { return _Tx_ch_Data.ToString(); }
            set
            {
                int temp;
                if (int.TryParse(value, out temp))
                {
                    m_control_txt_Tx_ch.EditorText = value;
                    _Tx_ch_Data = int.Parse(value);
                    Invalidate();
                }

            }
        }

        private string _nodeName = "Slave";
        [STNodeProperty("Title", "Title")]
        public string NodeName
        {
            get { return _nodeName; }
            set
            {
                this._nodeName = value;
                this.Title = this._nodeName;
            }
        }
        protected override void OnCreate()
        {
            base.OnCreate();
            this.TitleColor = Color.FromArgb(200, Color.DarkKhaki);
            this.Title = "Slave Node";
            this.AutoSize = false;
            this.Size = new Size(160, 140);

            m_op_out_IIC = this.OutputOptions.Add("IIC", typeof(IIC), false);
            m_op_out_Rx0 = this.OutputOptions.Add("Rx0", typeof(Rx0Tx0), false);
            m_op_out_Rx1 = this.OutputOptions.Add("Rx1", typeof(Rx1Tx1), false);
            m_op_out_Tx0 = this.OutputOptions.Add("Tx0", typeof(Tx0Rx0), false);
            m_op_out_Tx1 = this.OutputOptions.Add("Tx1", typeof(Tx1Rx1), false);
            m_op_out_B = this.OutputOptions.Add("B",typeof(AB), true);
            m_op_in_A = this.InputOptions.Add("A",typeof(AB), true);

            m_control_lbl_Rx_ch = new STNodeControl_Label();
            m_control_lbl_Rx_ch.DisplayRectangle = new Rectangle(20, 0, 80, 18);
            m_control_lbl_Rx_ch.EditorText = "Rx-ch";
            this.Controls.Add(m_control_lbl_Rx_ch);

            m_control_txt_Rx_ch = new STNodeControl_TextBox();
            m_control_txt_Rx_ch.DisplayRectangle = new Rectangle(50, 20, 40, 18);
            m_control_txt_Rx_ch.EditorText = Rx_ch_Data;
            this.Controls.Add(m_control_txt_Rx_ch);

            m_control_lbl_Tx_ch = new STNodeControl_Label();
            m_control_lbl_Tx_ch.DisplayRectangle = new Rectangle(20, 40, 80, 18);
            m_control_lbl_Tx_ch.EditorText = "Tx-ch";
            this.Controls.Add(m_control_lbl_Tx_ch);

            m_control_txt_Tx_ch = new STNodeControl_TextBox();
            m_control_txt_Tx_ch.DisplayRectangle = new Rectangle(50, 60, 40, 18);
            m_control_txt_Tx_ch.EditorText = Tx_ch_Data;
            this.Controls.Add(m_control_txt_Tx_ch);

            m_control_selectbox = new STNodeSelectEnumBox();
            m_control_selectbox.DisplayRectangle = new Rectangle(20, 100, 90, 18);
            m_control_selectbox.Enum = this._chipType;
            m_control_selectbox.ValueChanged += (s, e) =>
            {
                this._chipType = (ChipType)m_control_selectbox.Enum;
            };
            this.Controls.Add(m_control_selectbox);
        }
        protected override Point OnSetOptionDotLocation(STNodeOption op, Point pt, int nIndex)
        {
            if (op == m_op_in_A) return new Point(pt.X, pt.Y + 40);
            return base.OnSetOptionDotLocation(op, pt, nIndex);
        }
        protected override Rectangle OnSetOptionTextRectangle(STNodeOption op, Rectangle rect, int nIndex)
        {
            if (op == m_op_in_A) return new Rectangle(rect.X, rect.Y + 40, rect.Width, rect.Height);
            return base.OnSetOptionTextRectangle(op, rect, nIndex);
        }
        protected override void OnOwnerChanged()
        {
            base.OnOwnerChanged();
            if (this.Owner != null)
            {
                this.Owner.SetTypeColor(typeof(IIC), Color.DarkCyan);
                this.Owner.SetTypeColor(typeof(Tx0Rx0), Color.DarkBlue);
                this.Owner.SetTypeColor(typeof(Tx1Rx1), Color.SkyBlue);
                this.Owner.SetTypeColor(typeof(Rx0Tx0), Color.LightPink);
                this.Owner.SetTypeColor(typeof(Rx1Tx1), Color.DeepPink);
                this.Owner.SetTypeColor(typeof(AB), Color.Red);
            }
        }
    }
}
